package org.yellow.backend.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import org.yellow.backend.pojo.prescription;
import org.springframework.web.bind.annotation.*;
import org.yellow.backend.pojo.medicalrecords;
import org.yellow.backend.service.appointmentService;
import org.yellow.backend.service.prescriptionService;
import org.yellow.backend.service.recordsService;
import org.yellow.backend.utility.checkIdToStr;
import org.yellow.backend.utility.prescriptionStatus;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.Map;

/**
 * @author: hy
 * @create: 2024-01-01
 */
@RestController
@CrossOrigin
@SuppressWarnings({"all"})
@RequestMapping("/records")
public class recordsController {
    @Autowired
    private recordsService service;

    @Autowired
    private appointmentService appointmentService;
    @Autowired
    RestTemplate restTemplate;

    @GetMapping("/{id}")
    public medicalrecords selectRecordById(@PathVariable("id") int id){
        return service.selectRecordById(id);
    }

    @Autowired
    private prescriptionService prescriptionService;

    @PostMapping("/insert")
    @CrossOrigin
    public boolean insertNewRecord(@RequestBody Map<String,Object> data) throws ParseException {
        System.out.println(data);
        Long medicalNumber=Long.parseLong((String)data.get("medicalNumber"));
        String visitTime=(String)data.get("visitTime");
        String dataOfBirth=(String)data.get("dateOfBirth");
        String department=(String)data.get("department");
        Long patientId=Long.parseLong((String)data.get("patientId"));
        String patientName=(String)data.get("patientName");
        String gender=(String)data.get("gender");
        String cardNumber=(String)data.get("cardNumber");
        String medicalHistory=(String)data.get("medicalHistory");
        String diagnosisName=(String)data.get("diagnosisName");
        ArrayList<Boolean> checkups =(ArrayList<Boolean>) data.get("checkups");
        String prescription1=(String)data.get("prescription");
        prescription ps = new prescription(null,patientId,1L, Date.from(LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant()), prescriptionStatus.Active,prescription1);
        String checks = new checkIdToStr().util(checkups);
        // 插入处方表
        String url1="http://localhost:8888/prescription/insert";
//        ResponseEntity<Boolean> response1 = //restTemplate.postForEntity(url1, ps, Boolean.class);
        //
        boolean b = prescriptionService.insertNew(ps);
        // 定义日期格式
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        medicalrecords record = new medicalrecords(patientId, patientName, dateFormat.parse(dataOfBirth), gender, diagnosisName, dateFormat.parse("3434-43-90"), dateFormat.parse("9090-88-11"));
        service.insertNewRecord(record);


        return true;
    }
}
